package com.yjs.app.trade.web.controller.base;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yjs.app.trade.core.util.JedisUtil;
import com.yjs.app.trade.core.util.StringUtils;
import com.yjs.app.trade.service.service.sys.UserService;
import com.yjs.app.trade.service.service.util.AppSmsService;
import com.yjs.app.trade.web.controller.base.util.Result;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "identifying")
public class IdentifyingController extends BaseController {
	
	@Autowired
	AppSmsService appSmsService;
	
	@Autowired
	UserService userService;
	
	/**
	 * 发送手机验证码
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/send", method = RequestMethod.GET)
	@ResponseBody
	public Object sendPhoneCode(HttpServletRequest request, String data) {
		JSONObject jsonObject = null;
		if (StringUtils.isNotBlank(data)) {
			jsonObject = dpackData(data);
		}
		String mobile = "";
		if (jsonObject != null && jsonObject.get("mobile") != null) {
			mobile = jsonObject.getString("mobile");
		} else {
			mobile = userService.getUserInfo(getUserId(request)).get("mobile").toString();
		}
//		String code = VerifyCodeUtils.generateVerifyCode(6, mobileCodeType);
		String code = "111111";
		logger.debug(code);
		JedisUtil.setex(mobileCodeRedisHead + mobile, "{\"code\":\"" + code + "\", \"time\":\"" + System.currentTimeMillis() + "\"}", 60 * 5);
		appSmsService.sendMessage(mobile, code, "【易药通】您好，欢迎使用易药通，您的手机验证码是:", "修改手机号码");
		return Result.csuccess();
	}
}
